package C059.graph;

import java.util.Arrays;

/**
 * ClassName: AdjacencyMatrix
 * Package: C059.graph
 * Description: 邻接矩阵
 *
 * @Author BCXJ
 * @Create 2025/9/22 08:58
 * @Version 1.0
 * @Since 1.0
 */
public class AdjacencyMatrix  implements GraphInterface{
    int [][] graph;


    @Override
    public void init(int n, int m) {
        graph = new int[n + 1][n + 1];
        Arrays.fill(graph, -1);
    }

    /**
     * 初始化(下标就以1开始)
     * @param n 点数
     * @pa
     */

    public void init(int n) {
        graph = new int[n + 1][n + 1];
        for (int[] ints : graph) {
            Arrays.fill(ints, -1);
        }

    }


    /**
     * 加边
     * @param from
     * @param to
     * @param weight
     */
    @Override
    public void addEdge(int from, int to, int weight) {
        graph[from][to] = weight;
    }


    /**
     *     1(邻居、边权) : (5,5) (4,1)
     *     2(邻居、边权) : (4,6) (3,7) (5,4)
     *     3(邻居、边权) : (2,7) (4,2) (5,4)
     *     4(邻居、边权) : (2,6) (3,2) (1,1)
     *     5(邻居、边权) : (1,5) (2,4) (3,4)
     */
    @Override
    public void print() {
        System.out.println("=========邻接矩阵数组形式展现==========");
        for (int i = 1; i < graph.length; i++) {
            for (int j = 1; j < graph.length; j++) {
                System.out.print(graph[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("=========邻接矩阵标准输出==========");
        for (int i = 1; i < graph.length; i++) {
            System.out.print(i + "(邻边、边权)：");
            for (int j = 1; j < graph.length; j++) {
                if(graph[i][j] != -1) {
                    System.out.print("(" + j + ", " + graph[i][j] + ") ");
                }
            }
            System.out.println();
        }
    }


}
